package com.ruoyi.common.utils.hikvisionutils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.Response;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.ruoyi.common.utils.hikvisionutils.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: HikVisionUtils <br/>
 * Description: <br/>
 * date: 2020/12/30 13:41<br/>
 *
 * @author Hesion<br />
 * @since JDK 1.8
 */
public class HikVisionUtils {
    private static final Logger log = LoggerFactory.getLogger(HikVisionUtils.class);

    /**
     * 请根据自己的appKey和appSecret更换static静态块中的三个参数。
     */
    static {
        // artemis网关服务器ip端口
        ArtemisConfig.host = "116.9.120.142:9999";
        // 秘钥appkey
        ArtemisConfig.appKey = "26056044";
        // 秘钥appSecret
        ArtemisConfig.appSecret = "FGuZ8EHX6RjnyxLfnSEz";
    }

    /**
     * 能力开放平台的网站路径
     * 路径不用修改，就是/artemis
     */
    private static final String ARTEMIS_PATH = "/artemis";

    /**
     * @return com.hikvision.artemis.sdk.Response
     * @Author hst
     * @Description //TODO 获取区域列表接口可用来全量同步区域信息，返回结果分页展示。
     * @Date 13:46 2020/12/30
     * @Param [pageNo, pageNum]
     **/
    public static Response getRegionsByPage(RegionsByPage regionsByPage) {
        log.info("开始获取区域列表");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/regions";
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSON.toJSONString(regionsByPage);
            return HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    public static Response getSubRegionByRegion(SubRegion subRegion) {
        log.info("开始根据区域获取下级区域列表");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v2/regions/subRegions";
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSON.toJSONString(subRegion);
            return HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    public static Response getRegionsRoot() {
        log.info("开始获取根区域");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/regions/root";
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = "{}";
            Response response = HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
            return response;
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    public static Response getNodesByParams(NodesByParams nodesByParams) {
        log.info("开始根据查询条件查询区域列表信息");
        final String dataApi = ARTEMIS_PATH + "/api/irds/v2/region/nodesByParams";
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSONObject.toJSONString(nodesByParams);
            Response response = HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
            return response;
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    public static Response getSingleRegionInfo(SingleRegionRequest singleRegionRequest) {
        log.info("获取单个区域信息");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/region/indexCode/regionInfo";
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSONObject.toJSONString(singleRegionRequest);
            Response response = HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
            return response;
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    public static Response getSubResourcesByParams(ResourcesByParams resourcesByParams) {
        log.info("查询资源列表v2");
        final String dataApi = ARTEMIS_PATH + "/api/irds/v2/resource/resourcesByParams";
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSONObject.toJSONString(resourcesByParams);
            Response response = HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
            return response;
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    public static Response getSubResourcesByRegion(SubResourceByRegion subResourceByRegion) {
        log.info("根据区域获取下级资源列表v2");
        final String dataApi = ARTEMIS_PATH + "/api/irds/v2/resource/subResources";
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSONObject.toJSONString(subResourceByRegion);
            Response response = HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
            return response;
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    public static Response getResources(ResourceParams resourceParams) {
        log.info("获取资源列表v2");
        final String dataApi = ARTEMIS_PATH + "/api/irds/v2/deviceResource/resources";
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSONObject.toJSONString(resourceParams);
            Response response = HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
            return response;
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    public static Response getSingleResources(SingleRourceParams singleRourceParams) {
        log.info("获取单个资源信息");
        final String dataApi = ARTEMIS_PATH + "/api/irds/v1/resource/resource";
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSONObject.toJSONString(singleRourceParams);
            Response response = HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
            return response;
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    public static Response getEncodeDevice(Map<String, Object> params) {
        log.info("获取编码设备列表");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/encodeDevice/get";
        return doPostByMySelf(dataApi, params);
    }

    public static Response searchEncodeDevice(Map<String, Object> params) {
        log.info("查询编码设备列表");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/encodeDevice/search";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getSingleDevice(Map<String, Object> params) {
        log.info("获取单个编码设备信息");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/encodeDevice/single/get";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getSubRegionDevice(Map<String, Object> params) {
        log.info("根据区域获取下级编码设备列表");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/encodeDevice/subResources";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getCameraList(Map<String, Object> params) {
        log.info("查询监控点列表");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/camera/advance/cameraList";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getCamerasPage(Map<String, Object> params) {
        log.info("分页获取监控点资源");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/cameras";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getRegionCameras(Map<String, Object> params) {
        log.info("根据区域编号获取下级监控点列表");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/regions/regionIndexCode/cameras";
        return doPostByMySelf(dataApi, params);
    }

    public static Response getCamerasIndexCode(Map<String, Object> params) {
        log.info("根据编号获取监控点详细信息");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/cameras/indexCode";
        return doPostByMySelf(dataApi, params);
    }
    public static Response getBatch(Batch[] params) {
        log.info("add");
        final String dataApi = ARTEMIS_PATH + "/api/resource/v1/region/batch/add";
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSON.toJSONString(params);

            Response response = HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");

            if(null==response){
                for (int i = 0; i < 2; i++) {
                    response=HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
                    if (null != response){
                        break;
                    }
                }
                if (null==response){
                    response = new Response();
                    response.setStatusCode(500);
                    response.setErrorMessage("连接超时");
                    return response;
                }
            }
            return response;
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }

    /**
     * @return com.hikvision.artemis.sdk.Response
     * @Author hst
     * @Description //TODO 通用请求方法
     * @Date 10:53 2021/1/4
     * @Param [dataApi, params]  dataApi请求地址，params请求参数
     **/
    private static Response doPostByMySelf(String dataApi, Map<String, Object> params) {
        try {
            Map<String, String> path = new HashMap<String, String>(2) {
                {
                    put("http://", dataApi);
                }
            };
            String body = JSON.toJSONString(params);

            Response response = HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");

            if(null==response){
                for (int i = 0; i < 2; i++) {
                    response=HikVisionHttpUtils.doPostArtemis(path, body, null, null, "application/json");
                    if (null != response){
                        break;
                    }
                }
                if (null==response){
                    response = new Response();
                    response.setStatusCode(500);
                    response.setErrorMessage("连接超时");
                    return response;
                }
            }
            return response;
        } catch (Exception e) {
            Response response = new Response();
            response.setStatusCode(200);
            response.setErrorMessage("请求超时");
            return response;
        }
    }
}
